//jshint ignore:start
export default class DBTable {
	constructor(transaction, name) {
		this.table = transaction.objectStore(name); //获取数据表对象
	}
	/**
	 * 给当前表格添加数据
	 * @param data
	 * @returns { Promise<unknown> }
	 */
	async insert(data) {
		const result = this.table.add(data);
		return new Promise((resolve, reject) => {
			result.onsuccess = ({ target }) => resolve(target.result);
			result.onerror = reject;
		});
	}
	/**
	 * 删除当前表格数据
	 * @param key
	 * @returns { Promise<unknown> }
	 */
	async delete(key) {
		const result = this.table.delete(key);
		return new Promise((resolve, reject) => {
			result.onsuccess = ({ target }) => resolve(target.result);
			result.onerror = reject;
		});
	}
	/**
	 * 修改更新当前表格数据
	 * @param data
	 * @returns { Promise<unknown> }
	 */
	async update(data) {
		const result = this.table.put(data);
		return new Promise((resolve, reject) => {
			result.onsuccess = ({ target }) => resolve(target.result);
			result.onerror = reject;
		});
	}
	/**
	 * 查找当前表格数据
	 * @param key
	 * @returns { Promise<unknown> }
	 */
	async select(key) {
		const result = this.table.get(key);
		return new Promise((resolve, reject) => {
			result.onsuccess = ({ target }) => resolve(target.result);
			result.onerror = reject;
		});
	}
}
